import axios from "axios";

// 创建axios实例
const api = axios.create({
  baseURL: "/api",
  timeout: 10000,
  headers: {
    "Content-Type": "application/json",
  },
});

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 在这里可以添加认证信息等
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 博物馆相关API
export const museumApi = {
  // 获取博物馆列表
  getMuseums: () => api.get("/museums"),

  // 添加博物馆
  addMuseum: (data) => api.post("/museums", data),

  // 更新博物馆
  updateMuseum: (id, data) => api.put(`/museums/${id}`, data),

  // 删除博物馆
  deleteMuseum: (id) => api.delete(`/museums/${id}`),
};

// 票务相关API
export const ticketApi = {
  // 获取票种列表
  getTicketTypes: (museumId) => api.get(`/museums/${museumId}/tickets`),

  // 添加票种
  addTicketType: (museumId, data) =>
    api.post(`/museums/${museumId}/tickets`, data),

  // 更新票种
  updateTicketType: (museumId, ticketId, data) =>
    api.put(`/museums/${museumId}/tickets/${ticketId}`, data),

  // 删除票种
  deleteTicketType: (museumId, ticketId) =>
    api.delete(`/museums/${museumId}/tickets/${ticketId}`),
};

// 抢票相关API
export const bookingApi = {
  // 开始抢票
  startBooking: (data) => api.post("/booking/start", data),

  // 停止抢票
  stopBooking: (id) => api.post(`/booking/${id}/stop`),

  // 获取抢票状态
  getBookingStatus: (id) => api.get(`/booking/${id}/status`),

  // 获取抢票历史
  getBookingHistory: (params) => api.get("/booking/history", { params }),
};

// 身份信息相关API
export const identityApi = {
  // 获取身份信息列表
  getIdentities: () => api.get("/identities"),

  // 添加身份信息
  addIdentity: (data) => api.post("/identities", data),

  // 更新身份信息
  updateIdentity: (id, data) => api.put(`/identities/${id}`, data),

  // 删除身份信息
  deleteIdentity: (id) => api.delete(`/identities/${id}`),

  // 批量导入身份信息
  importIdentities: (data) => api.post("/identities/import", data),
};

// 联系方式相关API
export const contactApi = {
  // 获取联系方式列表
  getContacts: () => api.get("/contacts"),

  // 添加联系方式
  addContact: (data) => api.post("/contacts", data),

  // 更新联系方式
  updateContact: (id, data) => api.put(`/contacts/${id}`, data),

  // 删除联系方式
  deleteContact: (id) => api.delete(`/contacts/${id}`),

  // 批量导入联系方式
  importContacts: (data) => api.post("/contacts/import", data),
};

// 统计相关API
export const statisticsApi = {
  // 获取抢票成功率统计
  getSuccessRate: (params) => api.get("/statistics/success-rate", { params }),

  // 获取时段分布统计
  getTimeDistribution: (params) =>
    api.get("/statistics/time-distribution", { params }),

  // 获取响应时间分布统计
  getResponseTimeDistribution: (params) =>
    api.get("/statistics/response-time-distribution", { params }),
};

export default api;
